package com.hxrfid.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.system.domain.bo.SysTenantBo;
import com.hxrfid.system.domain.vo.SysTenantVo;
import com.hxrfid.system.service.ISysTenantService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 租户信息控制器
 * 前端访问路由地址为:/system/tenant
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController {

    private final ISysTenantService iSysTenantService;

    /**
     * 查询租户信息列表
     */
//    @SaCheckPermission("system:tenant:list")
    @GetMapping("/list")
    public TableDataInfo<SysTenantVo> list(SysTenantBo bo, PageQuery pageQuery) {
        return iSysTenantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户信息列表
     */
    @SaCheckPermission("system:tenant:export")
    @Log(title = "租户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysTenantBo bo, HttpServletResponse response) {
        List<SysTenantVo> list = iSysTenantService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户信息", SysTenantVo.class, response);
    }

    /**
     * 获取租户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:tenant:query")
    @GetMapping("/{id}")
    public R<SysTenantVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysTenantService.queryById(id));
    }

    /**
     * 新增租户信息
     */
    @SaCheckPermission("system:tenant:add")
    @Log(title = "租户信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTenantBo bo) {
        return toAjax(iSysTenantService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改租户信息
     */
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTenantBo bo) {
        return toAjax(iSysTenantService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除租户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:tenant:remove")
    @Log(title = "租户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysTenantService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
